package com.joshua.spring.integration.exception;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.joshua.spring.entity.vo.GlobalResponse;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.joshua.spring.entity.em.ResponseEnum.AUTHORITY_FORBIDDEN;

/**
 * com.joshua.spring.business.integration.exception -- IntegrationAccessDeniedHandler
 * description:
 *
 * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
 * @date 2020/1/14 11:22
 */
@Component
public class IntegrationAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ObjectMapper mapper = new ObjectMapper();
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        response.getWriter().write(mapper.writeValueAsString(GlobalResponse.buildFail(AUTHORITY_FORBIDDEN)));
    }
}
